/*	Copyright (C) 2012  Claude Richard
 *
 *	Optimist Racing is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Optimist Racing is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Optimist Racing.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

enum E_BUTTON {
	BUTTON_NOTHING,
	BUTTON_TURN_LEFT,
	BUTTON_TURN_RIGHT,
	BUTTON_UP,
	BUTTON_DOWN,
	BUTTON_SKID_LEFT,
	BUTTON_SKID_RIGHT,
	BUTTON_NUM
};

class cmKeyManager {

public:

	// The constructor and destructor are pretty standard.
	// The key mapping is set to something not very usable.
	cmKeyManager();
	~cmKeyManager();

	// TODO: load and save map from keyboard to button.
	//// This loads the key mapping from a file.
	//// If it fails then throws an error.
	//void load_mappings( const char* filename );
	//// Saves the key mappings.
	//// If it fails then throws an error.
	//void save_mappings( const char* filename ) const;


	// Links a key to a keytype.
	void set_mapping( unsigned char key, E_BUTTON keytype );
	void set_special_mapping( int key, E_BUTTON keytype );

	// Call this during the key-press event.
	void press_key( unsigned char key );
	void press_special_key( int key );
	// Call this during the key-release event.
	void release_key( unsigned char key );
	void release_special_key( int key );

	// This resets m_times_pressed and m_times_released
	// Call this after a frame has passed.
	void reset_counts();

	// Get methods for keys (cman_keys)
	int times_pressed( E_BUTTON button ) const;
	int times_released( E_BUTTON button ) const;
	bool is_pressed( E_BUTTON button ) const;

private:

	// These are arrays. Each array has one element per button
	// Each key has a number of times pressed since the last reset.
	// Each key has a number of times released since the last reset.
	int* m_times_pressed;
	int* m_times_released;
	bool* m_pressed;

	// This is the current key mapping from keyboard to cman-buttons.
	unsigned char *m_key_map;
	int *m_special_key_map;

};


class KeyboardInputs {

public:

	// The constructor and destructor are pretty standard.
	// The key mapping is set to something not very usable.
	KeyboardInputs();
	~KeyboardInputs();

	// Links a key to a keytype.
	void setMap( int key, E_BUTTON keytype );

	// Call this during the key-press event.
	void pressKey( int key );
	// Call this during the key-release event.
	void releaseKey( int key );

	// This resets m_times_pressed and m_times_released
	// Call this after a frame has passed.
	void resetCounts();

	// Get methods for keys and game-buttons
	int getKeyTimesPressed( int key ) const;
	int getKeyTimesReleased( int key ) const;
	bool isKeyPressed( int key ) const;
	int getButtonTimesPressed( E_BUTTON button ) const;
	int getButtonTimesReleased( E_BUTTON button ) const;
	bool isButtonPressed( E_BUTTON button ) const;

private:

	// These are arrays. Each array has one element per button
	// Each key has a number of times pressed since the last reset.
	// Each key has a number of times released since the last reset.
	int* mKeyTimesPressed;
	int* mKeyTimesReleased;
	bool* mKeyPressed;
	int* mButtonTimesPressed;
	int* mButtonTimesReleased;
	bool* mButtonPressed;

	// This is the current key mapping from keyboard to game-buttons.
	E_BUTTON* mMap;

};